//
//  HFBezierPathOrnament.m
//  HocusFocus
//
//  Created by Drew Davidson on 6/8/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "HFBezierPathOrnament.h"
#import "HFGraphicProperties.h"

static NSShadow *highlightShadow;

@implementation HFBezierPathOrnament

@synthesize bezierPath;
@synthesize bezierRect;

+ (void)initialize
{
    highlightShadow = [[NSShadow alloc] init];
    [highlightShadow setShadowColor:[NSColor keyboardFocusIndicatorColor]];
    [highlightShadow setShadowOffset:NSMakeSize(0.0, 0.0)];
    [highlightShadow setShadowBlurRadius:5.0];
}

- (id)initWithBezier:(NSBezierPath *)bezier inRect:(NSRect)rect
{
    self = [super init];
    self.bezierPath = bezier;
    self.bezierRect = rect;
    return self;
}

- (NSRect)drawingRectInside:(NSRect)focusArea
{
    NSRect result;
    
    result = NSInsetRect(focusArea, [graphicProperties strokeLineWidth], [graphicProperties strokeLineWidth]);
    if (NO && [graphicProperties shadow] != nil) {
        NSShadow *shadow = [graphicProperties shadow];
        CGFloat shadowHalf = ([shadow shadowBlurRadius] / 2.0);
        NSSize shadowOffset = [shadow shadowOffset];
//        NSRect shadowRect = result;
        NSSize adjustment = NSMakeSize(shadowOffset.width + shadowHalf, abs(shadowOffset.height) + shadowHalf);

        result.size.width -= adjustment.width;
        result.size.height -= adjustment.height;
    }
    return result;
}

- (NSBezierPath *)bezierPathInRect:(NSRect)r
{
    NSBezierPath *result = [[self.bezierPath copy] autorelease];
    CGFloat scaleX = r.size.width / bezierRect.size.width;
    CGFloat scaleY = r.size.height / bezierRect.size.height;
    CGFloat translateX = r.origin.x - bezierRect.origin.x;
    CGFloat translateY = r.origin.y - bezierRect.origin.y;
    NSAffineTransform *transform;
    
    transform = [NSAffineTransform transform];
    [transform translateXBy:translateX yBy:translateY];
    [transform scaleXBy:scaleX yBy:scaleY];
    [result transformUsingAffineTransform:transform];

    return result;
}

- (BOOL)hitTest:(NSPoint)pt inRect:(NSRect)focusArea
{
    return [[self bezierPathInRect:[self drawingRectInside:focusArea]] containsPoint:pt];
}

- (void)drawPath:(NSBezierPath *)path inRect:(NSRect)focusArea
{
    [[graphicProperties fillColor] set];
    [path fill];
    
    [[graphicProperties strokeColor] set];
    [path setLineWidth:[graphicProperties strokeLineWidth]];
    [path setLineCapStyle:[graphicProperties lineCapStyle]];
    [path stroke];
}

- (void)drawInRect:(NSRect)focusArea highlighted:(BOOL)yn
{
    NSBezierPath *path = [self bezierPathInRect:[self drawingRectInside:focusArea]];
    
    [[NSGraphicsContext currentContext] saveGraphicsState];
    if (yn) {
        [highlightShadow set];
    } else if ([graphicProperties shadow] != nil) {
        [[graphicProperties shadow] set];
    }
    [self drawPath:path inRect:focusArea];
    [self drawPath:path inRect:focusArea];
    [self drawPath:path inRect:focusArea];
    [[NSGraphicsContext currentContext] restoreGraphicsState];
    
    [self drawPath:path inRect:focusArea];
}

@end
